import { defineStore } from 'pinia'

import { userGetContact, userDetials, userEdit, userCounts } from '../../api/sysUser/index'

interface State {
  Contact: Contact;
  Detials: Detials;
  counts: number;
}

interface Detials {
  "user_id": string,
  "avatar": string,
  "email": string,
  "career_tags": string,
  "nickname": string,
  "introduction": string,
  "background_image": string,
  "role": string,
  "lock_status": string
}
interface Contact {
  "avatar": string,
  "email": string,
  "career_tags": string,
  "nickname": string,
  "introduction": string,
  "background_image": string,
  "lock_status": string,
  "articleCounts":number,
  "questionCounts":number,

}

export const UserStore = defineStore('user', {
  //属性
  state: (): State => {
    return {
      counts: 0,
      Contact: {
        "avatar": "",
        "email": "",
        "career_tags": "",
        "nickname": "",
        "introduction": "",
        "background_image": "",
        "lock_status": "",
        "articleCounts":0,
        "questionCounts":0,
      },
      Detials: {
        "user_id": "",
        "avatar": "",
        "email": "",
        "career_tags": "",
        "nickname": "",
        "introduction": "",
        "background_image": "",
        "role": "",
        "lock_status": ""
      }
    }
  },
  //数据处理
  getters: {


  },
  //函数
  actions: {
    //获取用户数量
    async getUserCounts() {
      try {
        let res: any = await userCounts();
        //赋值给存储
        this.counts = res.data.data
      }
      catch (error) {
        console.log("获取数目失败")
        return
      }

    },


    //获取用户简介
    async getUserContact(paramData: any) {
      try {
        let res: any = await userGetContact(paramData);
        //赋值给存储
        this.Contact = res.data.data
      }
      catch (error) {
        console.log("获取列表失败")
        return
      }

    },

    //获取用户全部信息
    async getUserDetials() {

      try {
        let res: any = await userDetials();
        //赋值给存储
        this.Detials = res.data.data
      }
      catch (error) {
        console.log("获取列表失败")
        return
      }

    },
    //修改信息
    async pullUserEdit(paramData: any) {

      try {
        await userEdit(paramData);
      }
      catch (error) {
        console.log("修改失败")
        return
      }

    },

  },
   // 开启数据缓存
   persist: {
    enabled: true,
    strategies: [
      {
        // 自定义名称
        key: 'users',
        // 保存位置，默认保存在sessionStorage
        storage: localStorage,
        // 指定要持久化的数据，默认所有 state 都会进行缓存，你可以通过 paths 指定要持久化的字段，其他的则不会进行持久化。
        // paths: ["counts"],
      },
    ],
  },
})